package com.tarena.lbs.coupon.controller;

import com.tarena.lbs.coupon.pojo.param.ActivityAddParam;
import com.tarena.lbs.coupon.pojo.query.ActivityPageQuery;
import com.tarena.lbs.coupon.pojo.vo.ActivityVO;
import com.tarena.lbs.coupon.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.api.pojo.dto.AdminDto;
import com.tarena.lbs.user.pojo.po.UserPO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * 活动相关功能接口
 */
@Slf4j
@RestController
@Tag(name = "活动服务", description = "活动服务")
public class ActivityController {

    @Autowired
    private ActivityService activityService;


    @Operation(summary = "商家活动列表")
    @PostMapping("/admin/marketing/marketingActivity/info/list")
    public Result<PageResult<ActivityVO>> pageList
            (@RequestBody ActivityPageQuery query,HttpSession session) throws Exception {
        //1.登录状态校验
        log.debug("正在使用过滤器进行登录状态校验...");
        UserPO userPO = (UserPO) session.getAttribute("user");
        if (userPO == null) {
            log.error("用户未登录");
            throw new ParseException("用户未登录", 0);
        }
        //2.商家信息校验
        log.debug("正在使用过滤器进行商家信息校验...");
        AdminDto adminDto = (AdminDto) session.getAttribute("admin");
        if (adminDto == null) {
            log.error("商家信息不存在");
            throw new ParseException("商家信息不存在", 0);
        }
        //3.活动列表查询-分页查询
        PageResult<ActivityVO> voPage = activityService.pageList(query);
        return new Result<>(voPage);
    }

    @Operation(summary = "商家活动保存")
    @PostMapping("/admin/marketing/marketingActivity/info/save")
    public Result<Void> activitySave(@RequestBody ActivityAddParam param, HttpSession session)
            throws Exception {
        //1.登录状态校验
        log.debug("正在使用过滤器进行登录状态校验...");
        UserPO userPO = (UserPO) session.getAttribute("user");
        if (userPO == null) {
            log.error("用户未登录");
            throw new ParseException("用户未登录", 0);
        }
        //2.商家信息校验
        log.debug("正在使用过滤器进行商家信息校验...");
        AdminDto adminDto = (AdminDto) session.getAttribute("admin");
        if (adminDto == null) {
            log.error("商家信息不存在");
            throw new ParseException("商家信息不存在", 0);
        }
        //3.活动保存业务
        activityService.activitySave(param);
        return Result.success();
    }

    @Operation(summary = "商家活动详情")
    @GetMapping("/admin/marketing/marketingActivity/info/detail/{id}")
    public Result<ActivityVO> activityDetail(@PathVariable("id") Integer id)
        throws BusinessException {
        ActivityVO vo = activityService.activityDetail(id);
        return new Result<>(vo);
    }

}
